﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace vensoft.DataAccess
{
    public abstract class MasterDataAcess
    {
        SqlConnection cnx;        

        public MasterDataAcess()
        {
            if (cnx == null)
            {
                cnx = new SqlConnection();
                cnx.ConnectionString = ConfigurationManager.ConnectionStrings["cnx"].ConnectionString;
            }
        }

        public SqlCommand CreateSelectCommand(string comando)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = comando;
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = cnx;
            return command;
        }

        public SqlDataReader ExecuteReader(SqlCommand command)
        {
            cnx.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        public void ExecuteCommand(SqlCommand command)
        {
            cnx.Open();
            command.ExecuteNonQuery();
            cnx.Close();
        }

        public void AddInParameter(SqlCommand command, string param,object value)
        {
            command.Parameters.AddWithValue(param, value);
        }

        public SqlCommand CreateCommand(string query)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = cnx;

            return command;
        }
       
    }
}

